<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="blank">
	<meta name="format-detection" content="telephone=no, email=no">
	<meta name="msapplication-tap-highlight" content="no">
	<link rel="stylesheet" type="text/css" href="/views/mobile/skin/css/style.css" />
	<title><{config_item('website_title')}></title>
</head>
<body>
<div class="wrap">
	<!-- 地址start -->
	<div id="address_list">
	</div>
	<!-- 地址start -->
	
	<!--地址列表模板start-->
	<script type='text/html' id='address_list_Template'>
		{{each result}}
		<div class="addcells">
			<div class="addListbox" id="add_list_{{$value.id}}">
				<div class="addTitlewrap flexbox">
					<div class="addNick">
						{{$value.full_name}}
					</div>
					<div class="addTel">
						{{$value.tel}}
					</div>
				</div>
				<div class="addTitlebox">
					{{$value.prov_str}}{{$value.city_str}}{{$value.area_str}}{{$value.address}}
				</div>
				<div class="addbtnbox flexbox">
					<label for="radio{{$value.id}}" class="addbtnL">
						<div style="font-size:0.26rem;">
							<div class="radioDbox">
						  		<input type="radio" onclick="set_default({{$value.id}},this);" name="add_radio" class="inputradio"  id="radio{{$value.id}}" {{if $value.is_default==1}}checked{{/if}}>
						  		<span></span>
						  	</div>
						设为默认地址
						</div>
					</label>
					<div class="addbtnR">
						<input type="button" value="编辑" class="inputaddbtn"  onclick="update_delete({{$value.id}})">
					</div>
					<div class="addbtnR">
						<input type="button" value="删除" class="inputaddbtn" onclick="address_delete({{$value.id}})">
					</div>
					<div class="addbtnR">
						<{if $redirect_url!=''}>
						<input type="button" value="选择" class="inputaddS" onclick="window.location.href='<{$redirect_url}>&address_id={{$value.id}}'">
						<{/if}>
					</div>
				</div>
			</div>
		</div>
		{{/each}}
	</script>
	<div class="infosub" id="address_addbtn">
		<input type="submit" value="新增地址"  class="infosubbtn" onclick="address_add();" >
	</div>
	<!--地址列表模板end-->
	<form action="<{site_url('/api/member/address/edit')}>" method="post" id="edit">
		<div id="add_address">

		</div>
	</form>
	<!--地址添加模板start-->
	<script type='text/html' id='address_edit_Template'>
		<input type="hidden" name="id" value="{{id}}">
		<div class="addcells">
			<div class="addListbox">
				<!--start  -->
				<div class="add_new  flexbox">
					<div class="addListL">
						收货人：
					</div>
					<div class="addListR flexbox">
						<input type="text" class="addinputText" name="full_name" datatype="*" nullmsg="收货人不能为空" value="{{full_name}}">
					</div>
				</div>
				<!-- end -->
				<!--start  -->
				<div class="add_new  flexbox">
					<div class="addListL">
						联系电话：
					</div>
					<div class="addListR flexbox">
						<input type="tel" class="addinputText" name="tel" datatype="m" nullmsg="电话不能为空" errormsg="手机号码格式错误" value="{{tel}}">
					</div>
				</div>
				<!--start  -->
				<div class="add_new  flexbox">
					<div class="addListL">
						所在地址：
					</div>
					<div class="addListR flexbox">
						<div class="selectbox">
							<select name="prov" class="select" onchange="area_list('city',this.value);">
							<option>省</option>
							</select>
						</div>
						<div class="selectbox">
							<select name="city" class="select" onchange="area_list('area',this.value);">
								<option>市</option>
							</select>
						</div>
						<div class="selectbox">
							<select name="area" class="select">
								<option>区</option>
							</select>
						</div>
					</div>
				</div>
				<!--start  -->
				<div class="add_new  flexbox">
					<div class="addListL">
						详细地址：
					</div>
					<div class="addListR flexbox">
						<input type="text" class="addinputText" name="address" datatype="*" nullmsg="详细地址不能为空" value="{{address}}">
					</div>
				</div>
			</div>
		</div>
		<div class="infosub">
			<input type="submit" value="确 认"  class="infosubbtn">
		</div>
	
	</script>
	</div>
<!--地址添加模板end-->
<script type="text/javascript" src="/public/js/jquery.js"></script>
<script type="text/javascript" src="/views/mobile/skin/js/public.js"></script>
<script type="text/javascript" src="/public/js/layer/layer.js"></script>
<script type="text/javascript" src="/public/js/artTemplate.js"></script>
<script type="text/javascript" src="/public/js/validform.js"></script>
<{include file="mobile/footer.html"}>
<script language="JavaScript">
	$(function() {
		//验证表单
		$.Tipmsg.r = null;
		$("#edit").Validform({
			tiptype: function (msg) {
				layer.msg(msg);
			},
			tipSweep: true,
			ajaxPost: true,
			callback: function (data) {
				if (data.status == "y") {
					window.location.reload();
				}
			}
		});

		//列出所有收货地址
		$.ajax({
			type:"POST",
			url: '<{site_url("/api/member/address")}>',
			data: '',
			dataType:"json",
			success: function(data){
				if (data.status == "y") {
					var html = template('address_list_Template', data);
					$('#address_list').append(html);
				}
			}
		});
	})

	//新增收货地址
	function address_add(){
		$('#address_addbtn').hide();
		var html = template('address_edit_Template');
		$('#add_address').html(html);
		area_list('prov', 0);//省份选择

	}

	//编辑收货地址
	function update_delete(id){
		$.ajax({
			type:"POST",
			url: '<{site_url("/api/member/address/get_id")}>',
			data: 'id='+id,
			dataType:"json",
			success: function(data){
				if (data.status == "y") {
					var html = template('address_edit_Template',data.result);
					$('#add_address').html(html);
					//省市区回填
					area_list('prov', 0, data.result.prov);//省份选择
					area_list('city', data.result.prov, data.result.city);//市区选择
					area_list('area', data.result.city, data.result.area);//地区选择
					$('body').scrollTop( $('body').height());//到页面底部
				}
			}
		});
	}

	//删除收货地址
	function address_delete(id){
		$.ajax({
			type:"POST",
			url: '<{site_url("/api/member/address/delete")}>',
			data: 'id='+id,
			dataType:"json",
			success: function(data){
				if (data.status == "y") {
					$("#add_list_"+id).remove()
				}
			}
		});
	}

	//地区加载area_name 选择框名称 parent_id 上级id default_id默认id
	function area_list(area_name, parent_id, default_id){
		$.ajax({
			type:"POST",
			url: '<{site_url("/api/area/get_list")}>',
			data: 'parent_id='+parent_id+'&default_id='+default_id,
			dataType:"html",
			success: function(data){
				$('[name="'+area_name+'"]').html(data);
			}
		});
	}

	//设置默认
	function set_default(id,obj){
		$.ajax({
			type:"POST",
			url: '<{site_url("/api/member/address/set_default")}>',
			data: 'id='+id,
			dataType:"json",
			success: function(data){
				if (data.status == "y") {
					$('#address_list [type="radio"]').prop('checked', false);
					$(obj).prop('checked', true);
					layer.msg('设置成功');
				}
			}
		});
	}
</script>
</body>
</html>